package com.gxa.autoparts.portal.controller;

import com.gxa.autoparts.core.common.JsonResult;
import com.gxa.autoparts.portal.service.CartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * @author R
 * @data 2022-01-10 9:26
 */
@RestController
@RequestMapping("/cart")
@Api("购物车接口")
public class CartController {

    @Autowired
    CartService cartService;

    @PostMapping("/add")
    @ApiOperation("添加购物车接口")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skuId",value = "具体的商品skuId",dataType = "Long",required = true,paramType = "query"),
            @ApiImplicitParam(name = "number",value = "商品数量",dataType = "Long",required = true,paramType = "query")
    })
    public Map add(Long userId,Long skuId,Long number){
        cartService.addCartInfo(userId,skuId,number);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/selectCart")
    @ApiOperation("查询购物车")
    public Map selectCart(Long userId){
        Map cart = cartService.getCart(userId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setData(cart);
        jsonResult.setMsg("查询成功");
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    @PostMapping("/addSkuNumber")
    @ApiOperation("增加购物车商品数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skuId",value = "需要修改数量的商品Id",required = true,paramType = "query",dataType = "Long"),
    })
    public Map addSkuNumber(Long userId,Long skuId){
        cartService.addSkuNumber(userId,skuId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/cutSkuNumber")
    @ApiOperation("减少购物车商品数量")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "skuId",value = "需要修改数量的商品Id",required = true,paramType = "query",dataType = "Long"),
    })
    public Map cutSkuNumber(Long userId,Long skuId){
        cartService.cutSkuNumber(userId,skuId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("修改成功");
        return jsonResult.getDataMap();
    }

    @PostMapping("/deleteCartInfo")
    @ApiOperation("删除购物车项")
    @ApiImplicitParam(name = "skuId",value = "需要删除的商品skuId",dataType = "Long",paramType = "query",required = true)
    public Map deleteCartInfo(Long userId,Long skuId){
        cartService.deleteCartInfo(userId, skuId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setMsg("删除成功");
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }
}
